﻿//===================================================================================
// $$
//
//
// Code By: Code Milker V2
// Date On: 2013//
//===================================================================================


namespace Forest.Lion.Data.SqlServer.Repositories
{
    using System;

    using System.Collections.Generic;

    using System.Data;

    using System.Linq;
    using Forest.Lion.Model;

    public class WorkRepository : BaseRepository, IWorkRepository
    {
        public WorkRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
        }
        public void Create(Work work)
        {
            this.GetDbSet<Work>().Add(work);
            this.UnitOfWork.SaveChanges();
        }

        public Work GetWork(Guid workId)
        {
            return this.GetDbSet<Work>()
                .Where(c => c.Id == workId)
                .Single();
        }

        public void Update(Work updatedWork)
        {
            this.GetDbSet<Work>().Attach(updatedWork);
            this.SetEntityState(updatedWork, updatedWork.Id == Guid.Empty
                ? EntityState.Added
                : EntityState.Modified);
            this.UnitOfWork.SaveChanges();
        }

        public void Delete(Guid workId)
        {
            Work workToDelete = this.GetWork(workId);
            this.GetDbSet<Work>().Remove(workToDelete);
            this.UnitOfWork.SaveChanges();
        }

        public IEnumerable<Work> Find()
        {
            return this.GetDbSet<Work>().Take(100)
                .ToList();
        }

        public IEnumerable<Work> GetAll()
        {
            return this.GetDbSet<Work>().Take(100)
                .ToList();
        }
    }
}
